//引入组件
import breadcrumb from '@/components/common/breadcrumb'
import redemp_details from '.././redemp_details'

import {getWinList,delWin,batchDelWin} from '@/api/api.js'
import {hasPermission} from '@/permission/btn'

export default {
    components:{
        breadcrumb,
        redemp_details,
    },
    data() {
        return {
            is_collapse: false,
            activeName:[],
            brand:[
                {name: 'lang.fun_exp'},
                {name: 'lang.form_exp'}
            ],
            hasIcon:false,
            advSearch:'te.advance_search',
            tableData:[

            ],
            isSelected: [],
            mulSelect:[],
            showDisabled:true,
            showDialog:false,
            total:0,
            currentPage: 1,
            pageCount: 0,
            pageSize:10,
            nameOrAge:'',//用于执行普通搜索
            level:'',
            winDate:'',
            salary:null,
            names:'',
            keysName:'',
            searchParams:{searchType:'0'},
            winDetails:{},
            status:'',
            levels:[
                {key:'1',label:'一等奖',value:'1'},
                {key:'2',label:'二等奖',value:'2'},
                {key:'3',label:'三等奖',value:'3'}
            ],
            statuss:[
                {key:'0',label:'待兑奖',value:0},
                {key:'1',label:'已兑奖',value:1}
            ]
        }
    },  
    methods: {
        //获取列表
        getFlowList:function(params){
            let _this = this;
            //访问后端 获取数据
            getWinList(params).then((result) => {
                //填充数据
                if(result.status == "200" && result.data.code == "0"){
                    let data = result.data.data;
                    _this.tableData = data.records;
                    _this.currentPage = data.current;
                    _this.pageSize = data.size;
                    _this.pageCount = data.pages;
                    _this.total = data.total;
                }else{
                    _this.$alert(result.data.msg,_this.$t('cm.tips'));
                }
            }).catch((err) => {
                _this.$alert(err,_this.$t('cm.tips'));
            });
        },
        //普通搜索
        commonSearch:function(){
            this.searchParams = {
                keyWord:this.keysName,
                searchType:1
            }
            let params = {
                pageIndex:1,
                pageSize:10,
            }
            //合并
            params = Object.assign(params,this.searchParams);
            this.getFlowList(params);
        },
        //高级搜索展开
        advanceSearch : function(){
            let _this = this;
            if(this.advSearch == "te.advance_search"){
                this.advSearch = "te.return";
            }else{
                this.advSearch = "te.advance_search";
            }
        },
        //重置数据
        resetData:function(){
            this.level = '';
            this.salary = null;
            this.names = '';
            this.status = '';
            this.winDate = null;
        },
        //高级搜索
        search:function(){
            this.searchParams = {
                winLevel:this.level,
                prizeAmount:this.salary,
                winName:this.names,
                winState:this.status,
                winDate:this.winDate,
                searchType:2,
            }
            let params = {
                pageIndex:1,
                pageSize:10,
            }
            //合并
            params = Object.assign(params,this.searchParams);
            this.getFlowList(params);
        },
        //改变每页显示数
        changeSize:function(pageSize){
            let params = {
                pageIndex:1,
                pageSize:pageSize
            }
            params = Object.assign(params,this.searchParams);
            this.getFlowList(params);
        },
        //翻页
        changeCurrentPage:function(current){
            let params = {
                pageIndex:current,
                pageSize:this.pageSize
            }
            params = Object.assign(params,this.searchParams);
            this.getFlowList(params);
        },
        //上一页
        prePage:function(current){
            let params = {
                pageIndex:current,
                pageSize:this.pageSize
            }
            params = Object.assign(params,this.searchParams);
            this.getFlowList(params);
        },
        //下一页
        nextPage:function(current){
            let params = {
                pageIndex:current,
                pageSize:this.pageSize
            }
            params = Object.assign(params,this.searchParams);
            this.getFlowList(params);
        },
        //每行的删除操作
        delData:function(row){
            let _this = this;
            this.$confirm("是否进行删除",_this.$t('cm.tips'),{
                type:'error',
                confirmButtonText: this.$t('te.confirm'),
                cancelButtonText: this.$t('te.cancel'),
                cancelButtonClass: 'btn-second',
                confirmButtonClass: 'btn-default',
            }).then(()=>{
                //逻辑删除  流程 撤回
                delWin({winId:row.cudWinId}).then((result) => {
                    if(result.status == "200" && result.data.code == "0"){
                        _this.$message({
                            message:'删除成功',
                            duration:1000,
                            showClose:true,
                        });
                        let params = {
                            searchType:0,
                            pageIndex:1,
                            pageSize:10,
                        }
                        _this.getFlowList(params);
                    }else{
                        _this.$alert(result.data.msg,_this.$t('cm.tips'));
                    }
                }).catch((err) => {
                    _this.$alert(err,_this.$t('cm.tips'));
                })
            }
                
            ).catch(function(){});       
        },
        //跳转到添加流程的界面
        addData:function(){
            this.$router.push("redemp_add");
        },
        //修改兑奖信息
        updateData:function(row){
            this.$router.push({path:"redemp_upd/id="+row.cudWinId,query:{obj:row}});
        },
        //展示详情
        showDetails:function(row){
            this.winDetails = row;
            this.showDialog = true;
        },
        //关闭详情对话框
        closeDialog:function(){    
            this.showDialog = false;
        },
        //全选或者取消全选
        checkSelected : function(val){
            //val 为更新后的值
            if(val == true){
                let rows = this.tableData;
                rows.forEach(row =>{
                    this.$refs.winTable.toggleRowSelection(row,true);
                });
            }else{
                this.$refs.winTable.clearSelection();
            }
        },
        //点击修改按钮
        updData:function(){
            let _this = this;
            if(this.mulSelect.length == 1){
                this.updateData(this.mulSelect[0]);
            }else{
                this.$alert("请选择一项进行修改",this.$t('cm.tips'));
            }
        },
        //用于全选操作
        selectChange:function(val){
            this.mulSelect = val;
        },
        //批量删除
        batchDel:function(rows){
            let _this = this;
            let objs = [];
            if(this.mulSelect.length > 0){
                for(let i = 0; i < this.mulSelect.length; i++){
                    objs.push(this.mulSelect[i]); 
                }
                this.$confirm("是否进行删除",_this.$t('cm.tips'),{
                    type:'error',
                    confirmButtonText: this.$t('te.confirm'),
                    cancelButtonText: this.$t('te.cancel'),
                    cancelButtonClass: 'btn-second',
                    confirmButtonClass: 'btn-default',
                }).then(
                    batchDelWin({objs:objs}).then((result) => {
                        if(result.status == "200" && result.data.code == "0"){
                            _this.$alert("删除成功",_this.$t('cm.tips'),{
                                callback:_=>{
                                    let params = {
                                        searchType:0,
                                        pageIndex:1,
                                        pageSize:10,
                                    }
                                    _this.getFlowList(params);
                                }
                            })
                        }else{
                            _this.$alert(result.data.msg,_this.$t('cm.tips'));
                        }
                    }).catch((err) => {
                        _this.$alert(err,_this.$t('cm.tips')); 
                    })
                ).catch(function(){})
                
            }else{
                this.$alert("请至少选择一项进行删除",this.$t('cm.tips'));
            }
        },
        //显示按钮
        showBtn(btn){
            return hasPermission(btn);
        },
    },
    mounted() {
        let params = {
            searchType:0,
            pageIndex:1,
            pageSize:10,
        }
        this.getFlowList(params);

    },  
    computed: {
        changeCollapse:function(){
          //用于对话框的对齐
          return this.$root.COLLAPSE;
        },
    },
    watch: {
        //监听 是否显示修改按钮
        mulSelect:function(){
            if(this.mulSelect.length != 1){
                this.showDisabled = true;
            }else{
                this.showDisabled = false;
            }
        }
    },
}